Optional Vertices.f


Requires Kagi
Requires OpenGL
Requires Seki
Requires OpenGL\ARB-VBO
Requires GL-Tools
Requires Matrix-math
Requires SORA\Transformations
Requires Dorothy.f

Only Forth Definitions

variable last-vertex-array
variable last-tcoord-array
variable last-color-array

0 Value vcolor
0 Value tcoord-x
0 Value tcoord-y
0 Value vpointer

macro: tcoord  to tcoord-y to tcoord-x ;
macro: +u   +to tcoord-x ;
macro: +v   +to tcoord-y ;

\ -----

type: vbo   end-type

type: vertexdef
   delegate glarrays
   delegate vertex
   delegate 2vertex
end-type

vertexdef extend: vertex2d
   short.field x
   short.field y
   short.field z
   short.skip
   short.field u
   short.field v
   rgba32.field color
end-type

vertexdef extend: point2d
   short.field x
   short.field y
end-type

\ -- Vertex2D --
\  For textured and colored primitives.
\  Actually DOES support 3D but it's got a low 16-bit resolution; it's INTENDED for 2D.

method: vertex2d.glarrays ( addr/ofs -- )
   glEnableClientState( GL_VERTEX_ARRAY )
   glEnableClientState( GL_COLOR_ARRAY )
   glEnableClientState( GL_TEXTURE_COORD_ARRAY )
   glVertexPointer( 3 GL_SHORT  16  x )
   glTexCoordPointer( 2 GL_SHORT  16  u )
   glColorPointer( 4 GL_UNSIGNED_BYTE  16  color ) ;

' vertex2d.glarrays >delegate vertex2d glarrays

\ Follows pairing rules and uses inlining for best speed
icode vertex2d.vertex ( x y z -- )
  &of vpointer [u] eax mov
  0 [ebp] ecx mov
  16bit: ebx 4 [eax] mov    \ z
  4 [ebp] ebx mov
  16bit: ecx 2 [eax] mov    \ y
  16bit: ebx 0 [eax] mov    \ x

  &of tcoord-x [u] ecx mov
  &of tcoord-y [u] ebx mov
  &of vcolor [u] edx mov
  16bit: ecx 8 [eax] mov    \ u
  16bit: ebx 10 [eax] mov   \ v
  edx 12 [eax] mov          \ color

  2 cells [ebp] ebx mov
  16 # eax add
  3 cells # ebp add
  eax &of vpointer [u] mov
  ret end-code

' vertex2d.vertex >delegate vertex2d vertex


\ -- Point2d --
\  For solid 2D shapes

icode point2d.2vertex  ( x y -- )
  &of vpointer [u] eax mov
  0 [ebp] ecx mov
  16bit: ebx 2 [eax] mov    \ y
  16bit: ecx 0 [eax] mov    \ x
  1 cells [ebp] ebx mov
  4 # eax add
  2 cells # ebp add
  eax &of vpointer [u] mov
  ret end-code
' point2d.2vertex >delegate point2d 2vertex

: point2d.vertex
   drop point2d.2vertex ;
' point2d.vertex >delegate point2d vertex

: point2d.glarrays   ( addr/ofs -- )
   locals|  a/o  |
   glEnableClientState( GL_VERTEX_ARRAY )
   glDisableClientState( GL_COLOR_ARRAY )
   glDisableClientState( GL_TEXTURE_COORD_ARRAY )
   glVertexPointer( 2 GL_SHORT  4  a/o  ) ;
' point2d.glarrays >delegate point2d vertex




0 vbo.ref mapped-vbo

: vbo.map  map-vbo ;
: vbo.unmap  unmap-vbo ;
: vbo.bind  bind-vbo ;
: vbo.unbind  unbind-vbo ;

\ Populate a VBO with data of a given size.  Address of the buffer is placed in VPOINTER.
\  If the vbo can't be mapped to client mem, then the memory is allocated and copied after you populate it.
\  Creates a new buffer each time, which lets the old buffer finish rendering without blocking.
\  If the OpenGL driver can map the VBO to client mem, rendering speed is 15% faster on my machine.
\ DEPRECRATED
-? 0 value xt
-? 0 vbo.ref v
-? 0 value s
:[] vbo.varray:[  (  size vbo xt -- [ A=addr -- ]  )
   to xt to v to s
   s  v.map  ?dup 0=  if
      s temp:[ dup >q  to vpointer  xt catch   glBufferSubDataARB( GL_ARRAY_BUFFER_ARB   0   s   q> )  throw  ;]
   else
      to vpointer  xt catch vbo.unmap throw
   then ;

0 transform.ref t

: quad2d ( u1 v1 u2 v2 z x1 y1 x2 y2 vertexdef -- )
   locals| vd y2 x2 y1 x1 z v2 u2 v1 u1 |
      u1 v1 tcoord x1 y1 z vd vertex
      u1 v2 tcoord x1 y2 z vd vertex
      u2 v2 tcoord x2 y2 z vd vertex
      u2 v1 tcoord x2 y1 z vd vertex ;

: transformed-quad2d  ( u1 v1 u2 v2 z x1 y1 x2 y2 vertexdef transform -- )
   to t locals| vd y2 x2 y1 x1 z v2 u2 v1 u1 |
      u1 v1 tcoord x1 y1 t.2transform z vd vertex
      u1 v2 tcoord x1 y2 t.2transform z vd vertex
      u2 v2 tcoord x2 y2 t.2transform z vd vertex
      u2 v1 tcoord x2 y1 t.2transform z vd vertex ;
